package com.example.demo.service.impl;


import java.util.List;
import java.util.Set;

import com.example.demo.dao.UserDao;
import com.example.demo.entity.User;
import com.example.demo.entity.UserAndRole;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.util.KeyUtil;
import com.example.demo.util.PasswordUtil;

public class UserServiceImpl implements UserService {
	
	private UserMapper userDao= new UserDao();

	public User createUser(User user){
		user.setId(KeyUtil.getRandomID());
		PasswordUtil.encryptPassword(user);
		String sqlId="com.example.demo.mapper.UserMapper.insertUser";
		userDao.createUser(sqlId, user);
		return user;
	}
	
	public int deleteUser(String id){
		String sqlId="com.example.demo.mapper.UserMapper.deleteUser";
		return userDao.deleteUser(sqlId, id);
	}
	
	public void changePassword(User user){
		PasswordUtil.encryptPassword(user);
		String sqlId="com.example.demo.mapper.UserMapper.updatePassword";
		userDao.updatePassword(sqlId, user);
	}
	
	public int correlateRoles(List<UserAndRole> userAndRoleList){
		String sqlId="com.example.demo.entity.UserAndRole.insertUserAndRole";
		return userDao.correlateRoles(sqlId, userAndRoleList);
	}
	
	
	public int uncorrelateRoles(UserAndRole userAndRole){
		String sqlId="com.example.demo.entity.UserAndRole.deleteUserAndRole";
		return userDao.uncorrelateRoles(sqlId, userAndRole);
	}
	
	public User findOne(String id){
		String sqlId="com.example.demo.mapper.UserMapper.selectOneUser";
		return userDao.findOne(sqlId, id);
	}
	
	public User findUserByUsername(String username){
		String sqlId="com.example.demo.mapper.UserMapper.selectUserByUsername";
		return userDao.findUserByUsername(sqlId, username);
	}
	
	public Set<String> findRoles(String username){
		String sqlId="com.example.demo.entity.UserAndRole.findRole";
		return userDao.findRoles(sqlId,username);
	}
	
	public Set<String> findPermissions(String username){
		String sqlId="com.example.demo.mapper.PermissionMapper.selectPermissionsByUsername";
		return userDao.findPermissions(sqlId, username);
	}
	
	
	public int deleteUserTable(){
		String sqlId="com.example.demo.mapper.UserMapper.deleteUserTable";
		return userDao.deleteUserTable(sqlId);
	}
	
}
